package atmgui;

import atm.FileUtil;
import atm.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * 存款
 */

public class SavePanel extends JPanel implements ActionListener {
    int b =0;
    JTextField usrTextField=new JFormattedTextField();

    private JButton offbtn=new JButton(new ImageIcon("src/images/buttonOff.jpg"));
    private ATM05 atm05;
    private  Font font =new Font("宋体",Font.PLAIN,25);
    private JButton surebtn=new JButton(new ImageIcon("src\\images\\buttonSure.jpg"));

    public  SavePanel(ATM05 atm05){
        this.atm05=atm05;


        JLabel label=new JLabel("请输入存款金额:");
        label.setBounds(100,200,300,50);
        label.setFont(font);//字体大小
        label.setForeground(Color.BLUE);//字体颜色
        //文本框组件


        usrTextField.setBounds(300,200,300,50);

        surebtn.setBounds(250,300,140,50);
        surebtn.addActionListener(this);

        offbtn.setBounds(500,300,140,50);
        offbtn.addActionListener(this);



        this.add(offbtn);
        this.add(surebtn);
        this.add(label);
        this.add(usrTextField);















        this.atm05=atm05;
        this.setLayout(null);
        this.setOpaque(false);






    }













    @Override
    public void actionPerformed(ActionEvent e) {
        if (surebtn==e.getSource()){
            User user = atm05.getLoginUser();
            try {
                user.setBlance(user.getBlance() + Double.parseDouble(usrTextField.getText()));
                FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
                usrTextField.setText("");
                JOptionPane.showMessageDialog(this,"存款成功");

            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"必须输入数字");
                return;

            }


            atm05.showPanel("menu");

        }
        else if (e.getSource()==offbtn){
            atm05.showPanel("menu");
        }


    }
}
